package Experiment2;

import java.io.FileWriter;
import java.util.Random;

import edu.princeton.cs.algs4.RandomSeq;

public class CreateDataset {
    
    public static void CreateIncreasing(String file_name,int n){
        FileWriter filewriter=null;
        try{
            filewriter=new FileWriter(".\\src\\Experiment2\\"+file_name);
            
            for(int i=1,j=0;i<=n;i++,j++){
                
                if(j>=100){ 
                    filewriter.append("\n");
                    j=0;
                }
                filewriter.append(Integer.valueOf(i).toString()+" ");
            }

            System.out.println("数据集："+file_name+"已生成成功");
        }catch(Exception e){
            System.out.println(e);
        }finally{
            try{
                if(filewriter!=null)
                    filewriter.close();
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }

    public static void CreateMostlyOrdered(String file_name,int n){
        FileWriter filewriter=null;
        try{
            filewriter=new FileWriter(".\\src\\Experiment2\\"+file_name);
            
            int original_data[]=new int[n];
            for(int i=0;i<n;i++){
                original_data[i]=i+1;
            }
            
            Random random=new Random();
            for(int i=0;i<0.15*n;i++){
                int a=random.nextInt(n);
                int b=random.nextInt(n);
                Exchange(original_data, a, b);
            }
            
            for(int i=0,j=0;i<n;i++,j++){
                
                if(j>=100){ 
                    filewriter.append("\n");
                    j=0;
                }
                filewriter.append(Integer.valueOf(original_data[i]).toString()+" ");
            }

            System.out.println("数据集："+file_name+"已生成成功");
        }catch(Exception e){
            System.out.println(e);
        }finally{
            try{
                if(filewriter!=null)
                    filewriter.close();
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }

    private static void Exchange(int data[],int i,int j){
        if(i==j) return;
        int t=data[i];
        data[i]=data[j];
        data[j]=t;
    }

    public static void CreateConstant(String file_name,int n){
        FileWriter filewriter=null;
        try{
            filewriter=new FileWriter(".\\src\\Experiment2\\"+file_name);
            
            Random random=new Random();
            int constant=random.nextInt(10);

            for(int i=1,j=0;i<=n;i++,j++){
                
                if(j>=100){ 
                    filewriter.append("\n");
                    j=0;
                }
                filewriter.append(Integer.valueOf(constant).toString()+" ");
            }

            System.out.println("数据集："+file_name+"已生成成功");
        }catch(Exception e){
            System.out.println(e);
        }finally{
            try{
                if(filewriter!=null)
                    filewriter.close();
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }

    public static void CreateRandom(String file_name,int n){
        FileWriter filewriter=null;
        try{
            filewriter=new FileWriter(".\\src\\Experiment2\\"+file_name);
            
            Random random=new Random();
            for(int i=1,j=0;i<=n;i++,j++){
                
                if(j>=100){ 
                    filewriter.append("\n");
                    j=0;
                }
                filewriter.append(Integer.valueOf(random.nextInt(n)+1).toString()+" ");
            }

            System.out.println("数据集："+file_name+"已生成成功");
        }catch(Exception e){
            System.out.println(e);
        }finally{
            try{
                if(filewriter!=null)
                    filewriter.close();
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }



    public static void main(String[] args) {
        CreateDataset.CreateIncreasing("data_increasing.txt", 10000);
        CreateDataset.CreateMostlyOrdered("data_mostlyordered.txt", 10000);
        CreateDataset.CreateConstant("data_constant.txt", 10000);

        CreateDataset.CreateRandom("data_random_shorter.txt", 1000);
        CreateDataset.CreateRandom("data_random_longer.txt", 10000);     
    }



    
}
